home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 435 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  94 lines

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Binaries
  5. Date: 4 Jan 1996 14:45:37 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4cgp6h$15vt@locutus.rchland.ibm.com>
  8. References: <peterf.58.00174FE2@gears.efn.org>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <peterf.58.00174FE2@gears.efn.org>, peterf@gears.efn.org (Peter F.) writes:
  14. >
  15. >#include <stdio.h>
  16. >
  17. >void main(void)
  18. > {
  19. >   FILE *in, *out;
  20. >
  21. >   if ((in = fopen("C:\\car.pcx", "rb"))== NULL)
  22. >    {
  23. >     fprintf(stderr, "Cannot open input file.\n");
  24. >    }
  25. >
  26. >   if ((out = fopen("C:\\temp\\car.pcx", "wb"))== NULL)
  27. >    {
  28. >     fprintf(stderr, "Cannot open output file.\n");
  29. >    }
  30. >
  31. >   while (!feof(in))
  32. >    { fputc(fgetc(in), out); }
  33.  
  34. The problem is right here with the copy loop.  The feof() function/macro
  35. tests for the EOF status bit.  However, this bit isn't set until _after_
  36. a read is attempted beyond the end of the file.  The EOF state is _not_ 
  37. set merely by reading the last byte, you must try to read one more...
  38.  
  39. Therefore, to terminate the loop fgetc() must read one extra byte and 
  40. fail at it.  However, by this time you've already tried to write that 
  41. byte. (I'll bet it is 0x1a -- EOF -- that is getting appended to your 
  42. files)
  43.  
  44. Try this, declare an int variable c up with the FILE*'s:
  45.  
  46.   int c;
  47.  
  48. Then code your loop as:
  49.  
  50.     while( ( c = fgetc( in ) ) != EOF )
  51.         fputc( c, out );
  52.  
  53. This way, you check to make sure that the read succeeded before doing 
  54. the write.
  55.  
  56. Of course, this _is_ a C++ group, why don't you try:
  57.  
  58. #include<fstream.h>
  59.  
  60. int main() {
  61.     ifstream infile( "C:\\car.pcx", ios::bin );  // may be ios::binary
  62.     ofstream outfile( "C:\\temp\\car.pcx", ios::bin );
  63.  
  64.     if( ! infile )
  65.         cerr << "Cannot open input file." << endl;
  66.     if( ! outfile )
  67.         cerr << "Cannot open output file." << endl;
  68.     outfile << infile.rdbuf();
  69.     return 0; }
  70.  
  71. >Also if anyone knows how to access Dos commands like copy or move from C++ 
  72. >this could help also.  I use BC++ 3.1.
  73.  
  74. Most C/C++ libraries support the system() command, you could also look 
  75. at spawnl() and/or execl().  Probably the simplest would be to just 
  76. enter:
  77.  
  78.     system( "copy c:\\car.pcx c:\\temp\\car.pcx" );
  79.  
  80. I think you need to #include dos.h or stdlib.h to get the system() 
  81. prototype.
  82.  
  83. >P.S. -- Anyone know how the copy command works, like how it keeps the same 
  84. >date, time, file size, of the target file you copied from.
  85.  
  86. That's not really a standard opperation (C/C++).  There is a DOS call 
  87. you can make to get/set a file's date.  You'll have to check a DOS 
  88. programming reference to find it. (or email me and I'll dig it up...)
  89.  
  90.  
  91. Phil Staite, team OS/2
  92. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  93.  
  94.